package com.zensun.system.domain;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构实体类
 *
 * @author gmk
 */
@Data
public class TreeSelect implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 节点ID
     */
    private Long id;

    /**
     * 节点名称
     */
    private String label;
    /**
     * pid
     */
    private Long pid;

    /**
     * 层级
     */
    private Integer level;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelect> children;

    public TreeSelect() {

    }

    public TreeSelect(SysMenu menu) {
        this.id = menu.getMenuId();
        this.label = menu.getMenuName();
        this.children = menu.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public TreeSelect(SysArea area) {
        if (StrUtil.isNotBlank(area.getAreaCode())) {
            this.id = Long.parseLong(area.getAreaCode());
        }
        this.label = area.getAreaName();
        if (StrUtil.isNotBlank(area.getAreaParentCode())) {
            this.pid = Long.parseLong(area.getAreaParentCode());
        }
        this.level = area.getLevel();
        this.children = area.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }
}
